package com.ds.infrastructure.hyperspace.console.model.response.deploy;

import com.ds.infrastructure.hyperspace.console.entity.table.TbDeployNode;
import com.ds.infrastructure.hyperspace.console.model.request.deploy.deploydata.HostGroupForm;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author: yaozhendong
 * @create: 2019-10-24 13:52
 **/
@Data
@NoArgsConstructor
public class GetHostGroupVO {
    private Long deployTaskId;
    private List<HostGroupForm> hostGroups;

    public static GetHostGroupVO toVO(Long deployTaskId, List<TbDeployNode> tbDeployNodeList) {
        List<HostGroupForm> hostGroupFormList = new ArrayList<>();
        for (TbDeployNode tbDeployNode : tbDeployNodeList) {
            Optional<HostGroupForm> first = hostGroupFormList.stream().filter(item -> item.getGroupName().equals(tbDeployNode.getHostGroupName())).findFirst();
            if (first.isPresent()) {
                continue;
            }
            HostGroupForm hostGroupForm = new HostGroupForm();
            hostGroupForm.setId(tbDeployNode.getHostGroupId());
            hostGroupForm.setGroupName(tbDeployNode.getHostGroupName());
            hostGroupFormList.add(hostGroupForm);

        }
        GetHostGroupVO result = new GetHostGroupVO();
        result.setDeployTaskId(deployTaskId);
        result.setHostGroups(hostGroupFormList);
        return result;
    }
}
